package com.fone.player.util;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fone.player.R;

public class ProgressDialogHelper {
	private static final String TAG = ProgressDialogHelper.class
			.getSimpleName();
	private static final ProgressDialogHelper mInstance = new ProgressDialogHelper();
	private DialogFragment mDialogFragment = null;

	public static ProgressDialogHelper getInstance() {
		return mInstance;
	}

	public void showDialog(FragmentActivity context) {
		if (mDialogFragment == null) {
			mDialogFragment = createProgressDialog(context);
		}
		mDialogFragment.show(context.getSupportFragmentManager(), TAG);
	}

	public void closeDialog() {
		if (mDialogFragment != null) {
			mDialogFragment.dismiss();
		}
	}

	/**
	 * 默认加载条
	 * 
	 * @param context
	 * @return
	 */
	private DialogFragment createProgressDialog(Context context) {
		DialogFragment dialogFragment = new DialogFragment() {
			@Override
			public View onCreateView(LayoutInflater inflater,
					ViewGroup container, Bundle savedInstanceState) {
				View view = inflater.inflate(R.layout.favourite_loading_layout,
						container, false);
				return view;

			}
		};
		dialogFragment.setStyle(R.style.progress_dialog,
				R.style.progress_dialog);
		dialogFragment.setCancelable(true);
		return dialogFragment;
	}

	/**
	 * 定制加载条
	 * 
	 * @param context
	 * @param text
	 *            定制内容
	 * @return
	 */
	@SuppressLint("InflateParams")
	private static Dialog createProgressDialog(Context context, String text) {
		View layout = LayoutInflater.from(context).inflate(
				R.layout.favourite_loading_layout, null);

		// 设置加载时显示的信息
		// TextView tvLoading = (TextView) layout
		// .findViewById(R.id.dialog_loading);
		// tvLoading.setText(text);

		Dialog dialog = new Dialog(context, R.style.progress_dialog);
		dialog.setContentView(layout);
		dialog.setCancelable(false);// 进度条加载时,不能中断请求
		return dialog;
	}
}
